package com.bite.blog.controller;

import com.bite.blog.contant.Constants;
import com.bite.blog.model.BlogInfo;
import com.bite.blog.service.BlogService;
import com.bite.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    // @GetMapping // 方法一: 只能发送get请求
    // @RequestMapping(value = "/getList",method = RequestMethod.GET) // 方法二: 只能发送get请求
    @RequestMapping(value = "/getList")
    public List<BlogInfo> getList(){
        log.info("获取博客列表");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail： {}",blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        // 判断登录用户和作者是否是同一人
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        // 从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId!=null&&userId==blogDetail.getUserId()){
            blogDetail.setLoginUser(true);
        }else {
            blogDetail.setLoginUser(false);
        }
        return blogDetail;
    }

    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request){  // 使用json接收
        log.info("添加博客,blogInfo: {}",blogInfo);
        // 参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle())
                ||!StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        // 获取登录用户
        // 获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        // 从token中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId==null||userId<=0){
            return false;
        }
        blogInfo.setUserId(userId);
        return blogService.insertBlog(blogInfo);
    }
    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客:blogInfo {}",blogInfo);
        if (blogInfo.getId()==null
                ||!StringUtils.hasLength(blogInfo.getContent())
                ||!StringUtils.hasLength(blogInfo.getTitle())){
            return false;
        }
        Integer result = blogService.update(blogInfo);
        if (result<=0) return false;
        return true;
    }
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除blog,blogId: {}",blogId);
        if (blogId<=0){
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer result = blogService.update(blogInfo);
        if (result<=0) return false;
        return true;
    }
}
